namespace IMPlant.Core.Data.Ado.BaseSupport
{
    using IMPlant.Core.Data.DataInterface;
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Runtime.InteropServices;

    public class DataOperationSurport
    {
        protected string _dbName = "";
        private SqlClientDataBase database;

        public DataOperationSurport(string dbName)
        {
            this._dbName = dbName;
            this.database = SqlClientDataBase.CreateDateBase(this._dbName);
        }

        private void BindParaToComm(DbCommand cmm, DbParameter[] paras)
        {
            if ((cmm != null) && (paras != null))
            {
                foreach (DbParameter parameter in paras)
                {
                    DbParameter parameter2 = (DbParameter) ((ICloneable) parameter).Clone();
                    cmm.Parameters.Add(parameter2);
                }
            }
        }

        public DbTransaction CreateDbTransaction()
        {
            return this.database.CreateTransactionSerializable();
        }

        protected object Excute(CommandAction action, DbTransaction dbt)
        {
            DbConnection openConncetion = null;
            object obj2;
            if (dbt == null)
            {
                openConncetion = this.database.GetOpenConncetion();
            }
            else
            {
                openConncetion = dbt.Connection;
            }
            DbCommand cmm = this.database.CreateCommand(action.type, action.commandText);
            cmm.Connection = openConncetion;
            this.BindParaToComm(cmm, action.parameters);
            if (dbt != null)
            {
                cmm.Transaction = dbt;
            }
            try
            {
                obj2 = action.DoInCommand(cmm);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (((openConncetion != null) && (openConncetion.State == ConnectionState.Open)) && (dbt == null))
                {
                    openConncetion.Close();
                }
            }
            return obj2;
        }
    }
}

